<template>
	<view>
		<city-select :activeCity="activeCity" :locationCity="locationCity" :obtainCitys="obtainCitys" :formatName="formatName"
		 :hotCity="hotCity" ref="city" @cityClick="selectCity"></city-select>
	</view>
</template>

<script>
	import citySelect from "@/components/city-select/city-select.vue"
	import WxMap from 'common/js/map-wx-jssdk.js'

	export default {
		components: {
			citySelect
		},
		data() {
			return {
				activeCity: null,
				locationCity: null,
				obtainCitys: [],
				hotCity: [{
						"id": 0,
						"title": "全国"
					},
					{
						"id": 110100,
						"title": "北京"
					},
					{
						"id": 140100,
						"title": "太原"
					},
					{
						"id": 120100,
						"title": "天津"
					},
					{
						"id": 130100,
						"title": "石家庄"
					},
					{
						"id": 370100,
						"title": "济南"
					}
				],
				formatName: 'title',
				cityAddon: 0
			}
		},
		computed: {
			location(){
				return this.$store.state.location
			},
		},
		watch:{
			location:{
			  handler: function(newVal) {
				if(newVal){
					this.getLocation()
				}
			  },
			  deep: true, // 开启深度监听
			},
		},
		onLoad(){
			if(this.location) this.getLocation()
		},
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();
			this.getCityList();
			this.checkAddonIsexit();
			if (uni.getStorageSync('city')) this.activeCity = uni.getStorageSync('city');
		},
		methods: {
			getCityList() {
				if (uni.getStorageSync('cityData')) this.obtainCitys = uni.getStorageSync('cityData');
				this.$api.sendRequest({
					url: '/api/address/city',
					success: res => {
						if (res.data) {
							this.obtainCitys = res.data;
							this.$refs.city.updateCitys(res.data);
							uni.setStorageSync('cityData', res.data);
						}
					}
				})
			},
			selectCity(data) {
				this.$api.sendRequest({
					url: '/api/website/getWebsiteidByAddress',
					data: {
						web_city: data.id
					},
					success: res => {
						if (res.code >= 0) {
							if (res.data) {
								uni.setStorage({
									key: 'city',
									data: data,
									success: () => {
										this.$util.goBack();
									}
								})
								uni.setStorage({
									key: 'website_id',
									data: res.data.site_id,
									success: () => {
										
									}
								}),
								uni.setStorage({
									key: 'websiteInfoStatus',
									data: res.data.status,
									success: () => {
										
									}
								})
							} else {
								this.$util.showToast({
									title: '该城市暂无站点，即将切换成全国'
								});
								
								setTimeout(() => {
									uni.setStorage({
										key: 'city',
										data: {id: 0, title: '全国'},
										success: () => {
											this.$util.goBack();
										}
									})
									uni.setStorage({
										key: 'website_id',
										data: 0,
										success: () => {
											
										}
									})
								}, 1500)
							}
						}
					}
				})
			},
			// 获取定位城市
			getLocation() {
				this.$api.sendRequest({
					url: '/api/address/cityByLocation',
					data: {
						location:  this.location.latitude + ',' + this.location.longitude
					},
					success: res => {
						if (res.data) {
							this.locationCity = res.data;
						}
					}
				});
			},
			checkAddonIsexit() {
				this.$api.sendRequest({
					url: '/api/addon/isexit',
					data: {
						name: 'city'
					},
					success: res => {
						this.cityAddon = res.data;
					}
				})
			}
		}
	}
</script>
